<?php
require_once 'IPNListener.php';
 
class YIIListener extends IPNListener
{
    protected $conn;
 
        protected function isVerifiedAmmount()
        {
            if($_POST['mc_gross'] == AMMOUNT)
            {
                return TRUE;
            }
 
            return FALSE;
        }
 
        protected function isNotProcessed()
        {
            $this->dbConnect();
            $sql = "SELECT * FROM utenti WHERE idTransazione=’$_POST[txn_id]’";
            $res = mysql_query($sql, $this->conn);
            if(mysql_num_rows($res))
            {
                return FALSE;
            }
            return TRUE;
        }
 
        protected function dbConnect()
        {
            $this->conn = @mysql_connect(HOST,DB_USER,DB_PASSWORD) OR die();
            @mysql_select_db(DB_NAME,$this->conn) OR die();
        }
 
        protected function getRandPassword()
        {
            $result = "";
            for($i = 0;$i < 10; $i++)
            {
                $chr = rand(40,126);
                $result .= chr($chr);
            }
            return $result;
        }
 
        protected function sendLoginData($password)
        {
            if(SIMULATION)
            {
                $to = ADMIN_MAIL;
                $add = "- SIMULAZIONE -";
            }
            else
            {
                $to = $_POST['payer_email'];
                $add = "";
            }
 
            $subject = "$add Attivazione account su Your Inspiration Images";
            $from = NO_REPLY;
            $message = "Ciao $_POST[first_name] e benvenuto su YII\r\n";
            $message .= "Ecco i tuoi dati di autenticazione:\r\n\r\n";
            $message .= "Nome utente: $_POST[payer_email] \r\n";
            $message .= "Password: $password \r\n\r\n";
            $message .= "Your Inspiration Images Team";
 
            mail($to,$subject,$message,"From: noreply<$from>");
        }
 
        public function insertNewUser()
        {
            if($this->isReadyTransaction())
            {
                $password = $this->getRandPassword();
                $md5password = md5($password);
 
                $sql = "INSERT INTO utenti
                (nome,cognome,email,username,password,idTransazione)
                VALUES ('$_POST[first_name]','$_POST[last_name]','$_POST[payer_email]','$_POST[payer_email]','$md5password','$_POST[txn_id]')";
 
                mysql_query($sql,$this->conn);
                $this->sendLoginData($password);
            }
        }
}
$ipn = new YIIListener();
$ipn->insertNewUser();
?>
